package cn.szzsi.business.controller.admin;

import cn.szzsi.common.bean.dto.CheckTreeNode;
import cn.szzsi.common.bean.dto.Msg;
import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.model.*;
import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengy on 2017/1/16.
 */
public class ActionController extends BaseAdminController<Action> {
    private Action action;
    private String shopId;
    public void list(){
        render(CommonUtil.TEMPLATE_PATH+"/business/action/action_list.html");
    }

    //ajax list
    public void ajaxlist(){
        shopId=getAttr("shopId");
        List<Action> lbam = Action.dao.find("select * from sys_action where shopId = ? order by createDate desc",shopId);
        for(Action bam:lbam){
            bam.init();
        }
        renderJson(lbam);
    }

    public void add(){
        render(CommonUtil.TEMPLATE_PATH+"/business/action/action_input.html");
    }
    public void edit(){
        String id = getPara("id","");
        if (StrKit.notBlank(id)){
            setAttr("action", Action.dao.findById(id));
        }
        render(CommonUtil.TEMPLATE_PATH+"/business/action/action_input.html");
    }

    @Before(Tx.class)
    public void save() {
        shopId=getAttr("shopId");
        action = getModel(Action.class);
        action.set("shopId",shopId);
        saved(action);
        renderJson(Ret.ok("info","添加成功!").set("url","list"));
    }

    public void update(){
        action = getModel(Action.class);
        action.update();
        //redirect("list");
        renderJson(Ret.ok("info","修改成功!").set("url","list"));

    }
    // 删除
    @Before(Tx.class)
    public void delete() {
        ids = getParaValues("ids");//动作ID
            for (String id : ids) {
                if(ActionRole.dao.getByActionId(id).size()>0){
                    renderJson(Ret.fail("info","删除失败,该动作已经被其他角色绑定,请先将其取消绑定才能删除!"));
                    return;
                }
                if(Action.dao.deleteById(id)){
                    renderJson(Ret.ok("info","删除成功!"));
                }else{
                    renderJson(Ret.fail("info","删除失败!"));
                }
            }

    }

    public void resourceTree(){
        shopId=getAttr("shopId");
        String id = getPara("id");//获取资源的id
        String actionId = getPara("actionId");//动作id

        List<Resource> lbr = Resource.dao.getByPid(id,shopId);
        List<CheckTreeNode> lctn = new ArrayList<CheckTreeNode>();
        for(Resource br:lbr){
            CheckTreeNode ctn = new CheckTreeNode();
            ctn.setName(br.getStr("name"));
            ctn.setId(br.getStr("id"));
            if(Action.dao.isActionResourceRalationExsit(actionId,br.getStr("id"))){
                ctn.setChecked(true);
            }else{
                ctn.setChecked(false);
            }

            if(Resource.dao.hasChildren(br.getStr("id"))){
                ctn.setIsParent(true);
            }else{
                ctn.setIsParent(false);
            }

            lctn.add(ctn);
        }
        renderJson(lctn);
    }

}
